home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / flagexp.y < prev    next >
Encoding:
Text File  |  1994-04-23  |  2.9 KB  |  139 lines

  1. %token NUMBER PHSTR TIMESTR ADDRSTR IDENT SPEED PHONE TIME ADDRESS DOW ANY WK WE SUN MON TUE WED THU FRI SAT EQ NE GT GE LT LE LB RB AND OR NOT XOR COMMA ASTERISK AROP LOGOP
  2.  
  3. %{
  4. #include <sys/types.h>
  5. #include <time.h>
  6. #include "lutil.h"
  7. #include "nodelist.h"
  8.  
  9. node *nodebuf;
  10. int result;
  11. struct tm *now;
  12. %}
  13.  
  14. %%
  15. fullline    : expression
  16.             {debug(13,"fulline: return %d",$1);result=$1;}
  17.         ;
  18. expression    : elemexp
  19.             {debug(13,"elem.expr %d",$1);$$ = $1;}
  20.         | NOT expression
  21.             {debug(13,"not.expr %d",$2);$$ = !($2);}
  22.         | expression LOGOP expression
  23.             {debug(13,"log.expr %d %d %d",$1,$2,$3);$$ = logic($1,$2,$3);}
  24.         | LB expression RB
  25.             {debug(13,"backeted.expr %d",$2);$$ = $2;}
  26.         ;
  27. elemexp        : flag
  28.             {debug(13,"flag %d",$1);$$ = match($1);}
  29.         | SPEED AROP NUMBER
  30.             {debug(13,"speed on num %d %d",$2,$3);$$ = checkspeed($2,$3);}
  31.         | PHONE PHSTR
  32.             {debug(13,"phone num %d",$2);$$ = checkphone();}
  33.         | PHONE NUMBER
  34.             {debug(13,"phone num %d",$2);$$ = checkphone();}
  35.         | TIME timestring
  36.             {debug(13,"time %d",$2);$$ = $2;}
  37.         | ADDRESS ADDRSTR
  38.             {debug(13,"address %d",$2);$$ = $2;}
  39.         ;
  40. flag        : IDENT
  41.             {debug(13,"ident %d",$1);$$ = $1;}
  42.         ;
  43. timestring    : TIMESTR
  44.             {debug(13,"timelem %d",$1);$$ = $1;}
  45.         | TIMESTR COMMA timestring
  46.             {debug(13,"log.expr %d %d %d",$1,OR,$3);$$ = logic($1,OR,$3);}
  47. %%
  48.  
  49. #include "flaglex.c"
  50.  
  51. int match(fl)
  52. int fl;
  53. {
  54.     int i;
  55.  
  56.     debug(13,"match: %d",fl);
  57.     if (fl == -1)
  58.     {
  59.         for (i=0;(i<MAXUFLAGS) && (nodebuf->uflags[i]);i++)
  60.             if (strcasecmp(yytext,nodebuf->uflags[i]) == 0) 
  61.                 return 1;
  62.         return 0;
  63.     }
  64.     else
  65.     {
  66.         return ((nodebuf->flags & fl) != 0);
  67.     }
  68. }
  69.  
  70. int logic(e1,op,e2)
  71. int e1,op,e2;
  72. {
  73.     debug(13,"logic: %d %d %d",e1,op,e2);
  74.     switch (op)
  75.     {
  76.     case AND:    return(e1 && e2);
  77.     case OR:    return(e1 || e2);
  78.     case XOR:    return(e1 ^ e2);
  79.     default:    logerr("Parser: internal error: invalid logical operator");
  80.             return 0;
  81.     }
  82. }
  83.  
  84. int checkspeed(op,speed)
  85. int op,speed;
  86. {
  87.     debug(13,"checkspeed: %d %d",op,speed);
  88.     switch (op)
  89.     {
  90.     case EQ:    return(nodebuf->speed == speed);
  91.     case NE:    return(nodebuf->speed != speed);
  92.     case GT:    return(nodebuf->speed >  speed);
  93.     case GE:    return(nodebuf->speed >= speed);
  94.     case LT:    return(nodebuf->speed <  speed);
  95.     case LE:    return(nodebuf->speed <= speed);
  96.     default:    logerr("Parser: internal error: invalid arithmetic operator");
  97.             return 0;
  98.     }
  99. }
  100.  
  101. int checkphone(void)
  102. {
  103.     debug(13,"checkphone: \"%s\"",yytext);
  104.     if (nodebuf->phone == NULL) return 0;
  105.     if (strncasecmp(yytext,nodebuf->phone,strlen(yytext)) == 0) return 1;
  106.     else return 0;
  107. }
  108.  
  109. int flagexp(expr,nl)
  110. char *expr;
  111. node *nl;
  112. {
  113.     time_t tt;
  114.  
  115.     debug(13,"check expression \"%s\"",expr);
  116.     nodebuf=nl;
  117.     (void)time(&tt);
  118.     now=localtime(&tt);
  119.     yyPTR=expr;
  120. #ifdef FLEX_SCANNER  /* flex requires reinitialization */
  121.     yy_init=1;
  122. #endif
  123.     result=0;
  124.     if ((yyparse()))
  125.     {
  126.         logerr("could not parse expression \"%s\", assume `false'",expr);
  127.         return 0;
  128.     }
  129.     debug(13,"checking result is \"%s\"",result?"true":"false");
  130.     return result;
  131. }
  132.  
  133. int yyerror(s)
  134. char *s;
  135. {
  136.     logerr("parser error: %s",s);
  137.     return 0;
  138. }
  139.